package com.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class downLoad extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public downLoad() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
	
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		 String fileName = request.getParameter("filename");  
		 String phone=request.getParameter("phone");
	        fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
		        //�ϴ����ļ����Ǳ�����/WEB-INF/uploadĿ¼�µ���Ŀ¼����
		         String fileSaveRootPath=this.getServletContext().getRealPath("/WEB-INF/upload");
		         //ͨ���ļ����ҳ��ļ�������Ŀ¼
		         
		          String path = phonelib(fileSaveRootPath,phone,fileName);
		          //�õ�Ҫ���ص��ļ�
		         
		          File file = new File(path);
		          //����ļ�������
		          if(!file.exists()){
		             request.getRequestDispatcher("/message.jsp").forward(request, response);
		              return;
		          }
		          //�����ļ���
		          String realname = fileName.substring(fileName.indexOf("_")+1);
		          //������Ӧͷ��������������ظ��ļ�
		          response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
		          //��ȡҪ���ص��ļ������浽�ļ�������
		          FileInputStream in = new FileInputStream(path);
		          //���������
		          OutputStream out = response.getOutputStream();
		          //����������
		          byte buffer[] = new byte[1024];
		          int len = 0;
		         //ѭ�����������е����ݶ�ȡ������������
		          while((len=in.read(buffer))>0){
		              //��������������ݵ��������ʵ���ļ�����
		             out.write(buffer, 0, len);
		         }
		         //�ر��ļ�������
		     in.close();
		          //�ر������
		          out.close();
		     }
		    
	

		

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	
private String phonelib(String savePath,String phone,String fileName){
	
		
		String dir=savePath+"/"+phone+"/"+fileName;
		File file=new File(dir);
		if(!file.exists()){
			
			file.mkdirs();

		}
		return dir;
	}

}
